<template>
	<view style="padding-bottom: 100upx;" class="address">

		<uni-nav-bar fixed="true" left-icon="back" :status-bar="true" :border="false" title="地址编辑" class="NavBar"
			@clickLeft="NavBar()">
		</uni-nav-bar>

		<view v-if="true" class="editAddress">
			<view class="topLine"></view>
			<!-- <view class="line11 editCommon">收货人信息</view> -->
			<view class="editCommon">
				<text style="width: 136upx; display: inline-block;" class="fl">收货人</text><input placeholder="请输入姓名"
					v-model="editAddress.name" maxlength="15" class="uni-input fl commonInput" />
			</view>
			<view class="editCommon">
				<text style="width: 136upx; display: inline-block;" class="fl">手机号</text><input placeholder="请输入手机号码"
					v-model="editAddress.mobile" maxlength="11" type="number" class="uni-input fl commonInput" />
			</view>
			<!-- <view style="display: none;" class="editCommon line11">所在地区</view> -->
			<view @click="openAddres" class="editCommon" >
				<text style="width: 136upx; display: inline-block;" class="fl">所在地区</text>
				<text :style="{color: address.length == 0 ? '#666' : '#333'}" class="uni-input fl commonInput">{{address.length == 0 ? "请选择所在地区" : address}}</text>
				<!-- <input disabled="disabled" @click="openAddres" v-model="address" placeholder="请选择所在地区" type="text"
					class="uni-input fl commonInput" /> --> 
			</view>
			<view class="editCommon">
				<text style="width: 136upx; display: inline-block;" class="fl">详细地址</text><input placeholder="请输入详细地址"
					v-model="editAddress.address_xx" class="uni-input fl commonInput" />
			</view>
			<view class="editCommon" @click.stop="isCheck()">
				<text style="width: 136upx; display: inline-block;" class="fl">设置默认</text>
				<image :src="isdefault?`${picUrl}/Switch-On.png`:`${picUrl}/Switch-Off.png`" mode=""></image>
			</view>
			<button @click="ok()" class="addAddress" type="">确认</button>
		</view>

		<view class="topLine"></view>
		<!-- :pickerValueDefault="cityPickerValueDefault" -->
		<simple-address ref="simpleAddress" @onConfirm="onConfirm" themeColor="#007AFF"></simple-address>
	</view>


</template>

<script>
	import simpleAddress from '@/components/simple-address/simple-address.nvue';
	// import provinceData from '@/components/simple-address/city-data/province2.js';
	// import cityData from '@/components/simple-address/city-data/city2.js';
	// import areaData from '@/components/simple-address/city-data/area2.js';
	export default {
		components: {
			simpleAddress
		},
		data() {
			return {
				addressId: '',
				member_id: "",
				member_name: "",
				//三级联动
				cityPickerValueDefault: [0, 0, 1],
				pickerText: '',

				//add数据
				add_provinceCode: "",
				add_cityCode: "",
				add_areaCode: "",

				key: "",
				editIndex: 0,
				from: 0, //0新增，1修改
				from2: "",
				editFlag: false,
				address: "",
				editAddress: {
					"id": 999,
					"name": "",
					"tel": "",
					"mobile": "",
					"address_sheng": " ",
					"address_shi": " ",
					"address_qu": " ",
					"address_xx": "",
					"isCheck": "false"
				},
				isdefault: false,
				addressLis: [],
				cart_id: "",
				ifcart: "",
				is_global: "",
				order_type: "",
				SpellGroup_sn: "",
				state: "",
			}

		},

		onLoad(options) {
			if (options.cart_id) {
				this.cart_id = options.cart_id
				this.ifcart = options.ifcart
				this.is_global = options.is_global
				this.order_type = options.order_type
				this.SpellGroup_sn = options.SpellGroup_sn
				this.state = options.state
			}
			if (options.isdefault) {
				if (options.isdefault == 0) {
					this.isdefault = false
				} else if (options.isdefault == 1) {
					this.isdefault = true
				}
			}

			this.from = options.from
			if (this.from == 1) {
				this.from = 1

				var aa = JSON.parse(options.address_info)
				this.addressId = aa.address_id
				this.editAddress.name = aa.true_name
				this.editAddress.tel = aa.tel_phone
				this.editAddress.mobile = aa.mob_phone
				this.address = aa.area_info.replace(/,/g, " ")
				this.editAddress.address_xx = aa.address
				this.add_provinceCode = aa.address_id
				this.add_cityCode = aa.city_id
				this.add_areaCode = aa.area_id
				this.key = uni.getStorageSync('key');
				this.member_id = uni.getStorageSync('api_member_id')
				this.member_name = uni.getStorageSync('api_member_name')
				this.from2 = options.editOrAdd;

			} else {
				this.from = 0
			}
			if (uni.getStorageSync("key")) {
				this.getAddressData()
			} else {
				this.isdefault = true
			}

		},


		methods: {

			getAddressData() {
				let that = this;
				this.$util.request({
					url: '/mobile/index.php?act=member_address&op=address_list',
					ToLogin: false,
					method: 'post',
					data: {},
				}).then(function(res) {
					if (res.datas.address_list.length <= 0) {
						that.isdefault = true
					}
				})
			},
			isCheck() {
				this.isdefault = !this.isdefault;
			},
			//增加地址
			addAddressData() {
				let that = this;
				if (!that.address) {
					uni.showToast({
						title: "请选择地址",
						icon: "none"
					})
					return false
				}
				this.$util.request({
					url: '/mobile/index.php?act=member_address&op=address_add',
					method: 'post',
					data: {
						"api_member_name": that.member_name,
						"key": that.key,
						"member_id": that.member_id,
						"true_name": that.editAddress.name.trim(),
						"country_id": "0",
						"province_id": that.add_provinceCode,
						"city_id": that.add_cityCode,
						"area_id": that.add_areaCode,
						"area_info": that.editAddress.address_sheng + " " + that.editAddress.address_shi + " " +
							that.editAddress.address_qu,
						"address": that.editAddress.address_xx,
						"mob_phone": that.editAddress.mobile,
						"code": "00086",
						"tel_phone": that.editAddress.tel,
						is_default: that.isdefault ? 1 : 0,
						unionid: uni.getStorageSync("unionId") || "",
						openid: uni.getStorageSync("openid") || "",
						nickname: uni.getStorageSync("nickname") || "",
					},
				}).then((res) => {
					if (res.error_code == 0) {
						if (this.state) {
							// uni.setStorageSync("key", res.datas.key);
							// uni.setStorageSync("api_member_name", res.datas.username)
							// uni.setStorageSync("api_member_id", res.datas.member_id)
							// if(res.datas.openid){
							// 	uni.setStorageSync("openid", res.datas.openid)
							// }
							// uni.redirectTo({
							// 	url:'/pagesA/order/Confirmation?cart_id='+this.cart_id+'&ifcart='+this.ifcart+'&is_global='+this.is_global+'&order_type='+this.order_type+'&SpellGroup_sn='+this.SpellGroup_sn
							// })
							uni.navigateBack()
						} else {
							uni.navigateBack()
						}
					} else {
						uni.showToast({
							title: res.message,
							icon: "none"
						})
					}
				})
			},


			////三级联动
			openAddres() {
				this.$refs.simpleAddress.open();
			},
			onConfirm(e) {
				this.pickerText = JSON.stringify(e);
				this.editAddress.address_sheng = e.labelArr[0];
				this.editAddress.address_shi = e.labelArr[1];
				if (e.labelArr.length > 2) {
					this.editAddress.address_qu = e.labelArr[2];
				}
				this.address = e.label;
				this.add_provinceCode = JSON.parse(this.pickerText).provinceCode;
				this.add_cityCode = JSON.parse(this.pickerText).cityCode;
				this.add_areaCode = JSON.parse(this.pickerText).areaCode;
			},


			// isCheck(index,num){
			// 	this.setCheck(num)
			// },

			//新增地址
			addAddress(num) {
				uni.navigateTo({
					url: 'add'
				})
			},

			ok() {
				var that = this;
				if (this.from == 0) {
					if (that.editAddress.name.trim().length == 0) {
						uni.showToast({
							title: "收货人信息不能为空",
							icon: "none"
						})
						return false;
					}
					if (that.add_areaCode.trim().length == 0) {
						uni.showToast({
							title: "收货人地址不能为空",
							icon: "none"
						})
						return false;
					}
					if (that.editAddress.mobile.trim().length == 0) {
						uni.showToast({
							title: "收货人手机号不能为空",
							icon: "none"
						})
						return false;
					}
					if (!(/^1[3456789]\d{9}$/.test(that.editAddress.mobile))) {
						uni.showToast({
							title: "手机号码有误，请重填",
							icon: "none"
						})
						return false;
					}
					if (this.from2 == 'add') {
						if (that.editAddress.address_sheng.trim().length == 0) {
							uni.showToast({
								title: "请选择地址",
								icon: "none"
							})
							return false;
						}
					}
					if (that.editAddress.address_xx.trim().length == 0) {
						uni.showToast({
							title: "收货人详细地址不能为空",
							icon: "none"
						})
						return false;
					}
					// this.addressLis.push(this.editAddress)
					this.addAddressData();

				}
				if (this.from == 1) {
					console.log("编辑");
					if (!(/^1[3456789]\d{9}$/.test(that.editAddress.mobile))) {
						uni.showToast({
							title: "手机号码有误，请重填",
							icon: "none"
						})
						return false;
					}
					this.$util.request({
						url: '/mobile/index.php?act=member_address&op=address_edit',
						method: 'post',
						data: {
							"address_id": that.addressId,
							"true_name": that.editAddress.name.trim(),
							"city_id": that.add_cityCode,
							"area_id": that.add_areaCode,
							"area_info": that.address.replace(/-/g, " "),
							"address": that.editAddress.address_xx,
							"mob_phone": that.editAddress.mobile,
							"tel_phone": that.editAddress.tel,
							"code": "00086",
							"key": that.key,
							is_default: that.isdefault ? 1 : 0
						},
					}).then(function(res) {

						that.editFlag = false
						uni.navigateBack({
							delta: 1
						});
					})
				}

			},
			NavBar() {
				uni.showModal({
					title: '提示',
					content: '您没有保存是否返回上一页',
					success: function(res2) {
						if (res2.confirm) {
							uni.navigateBack({})
						} else if (res2.cancel) {

						}
					}
				})
			}


		}
	}
</script>

<style>
	.isCheck image {

		width: 54upx;
		height: 54upx;
		position: relative;
		top: 20upx;
		margin-right: 10upx;
		margin-left: 30rpx;
	}

	.popup-btn {
		margin-top: 10px;
	}

	.commonInput {
		width: 500upx;
		position: relative;
		top: 0upx;
		height: 50upx;
		text-align: right;
		border: 1px solid #red;
	}

	.editCommon {
		padding: 0 40upx;
		font-size: 32rpx;
		font-family: PingFang SC-Regular, PingFang SC;
		font-weight: 400;
		color: #323232;
		line-height: 90upx;
		border-bottom: 2upx solid #f5f5f5;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.editCommon image {
		width: 100rpx;
		height: 60rpx;
	}

	.fl {
		float: left;
	}

	.fr {
		float: right;
	}

	.fs24C333 {
		font-size: 24upx;
		color: #333;
	}

	uni-page-body {
		background-color: #FFFFFF;
		color: rgb(102, 102, 102);
	}

	.topLine {
		width: 710upx;
		height: 110upx;
		margin: 0 auto;
		/* #ifdef APP-PLUS */
		height: 180upx;
		/* #endif */
		/* #ifdef MP-WEIXIN */
		height: 150rpx;
		/* #endif */
		/* background-color: #f5f5f5; */
	}

	.addressLi {
		background-color: #FFFFFF;
		width: 710upx;
		margin: 0 auto;
		border-bottom: 2upx solid #e5e5e5;
		padding: 38upx 38upx 88upx 38upx;

	}

	.line5 {
		width: 670upx;
	}

	.line5 image {
		width: 54upx;
		height: 54upx;
		position: relative;
		top: 20upx;
		margin-right: 10upx;
	}

	.addAddress {
		width: 640rpx;
		height: 88rpx;
		background: linear-gradient(138deg, #EA8C1E 0%, #E84A1D 100%);
		border-radius: 50rpx 50rpx 50rpx 50rpx;
		font-size: 34rpx;
		font-family: PingFang SC-Regular, PingFang SC;
		font-weight: 400;
		color: #FFFFFF;
		margin-top: 170rpx;
		text-align: center;
		line-height: 88rpx;
	}

	.editAddress {
		position: fixed;
		z-index: 1;
		left: 0;
		top: 0;
		right: 0;
		bottom: 0;
		width: 100%;
		height: 100%;
		background-color: #fff;
		color: #333333;
	}

	.line11 {
		color: #333333;
		font-size: 30upx;
		font-weight: bold;
	}





	.fs30 {
		/* font-size: 30upx */
		font-size: 45upx
	}

	.fs26 {
		font-size: 39upx
			/* font-size: 26upx */
	}

	.fs12 {
		font-size: 18upx
			/* font-size: 12upx */
	}

	.fs16 {
		font-size: 24upx
			/* font-size: 16upx */
	}

	.fs14 {
		font-size: 21upx
			/* font-size: 14upx */
	}

	.fs15 {
		font-size: 22.5upx
			/* font-size: 15upx */
	}

	.fs27 {
		font-size: 40.5upx
			/* font-size: 27upx */
	}

	.fs18 {
		font-size: 27upx
			/* font-size: 18upx */
	}

	.fs28 {
		font-size: 42upx
			/* font-size: 28upx */
	}

	.fs10 {
		font-size: 15upx
			/* font-size: 10upx */
	}

	.fs30 {
		font-size: 45upx
			/* font-size: 28upx */
	}

	.fs24 {
		font-size: 36upx
			/* font-size: 24upx */
	}

	.fs20 {
		font-size: 30upx
			/* font-size: 20upx */
	}

	.fs22 {
		font-size: 33upx
			/* font-size: 22upx */
	}

	.fs21 {
		font-size: 31.5upx
			/* font-size: 21upx */
	}

	.twoline {
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-line-clamp: 2;
		-webkit-box-orient: vertical;
	}
</style>